#include "CustomStyledItemDelegate.h"
#include "ItemDefine.h"

#include <QPainter>
#include <QStyleOptionViewItem>

CustomStyledItemDelegate::CustomStyledItemDelegate(QObject *parent) : QStyledItemDelegate{parent}
{}



    void CustomStyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (index.isValid()) {
        painter->save();


        // this->paint1(painter, option, index);
        this->paint2(painter, option, index);

        // 调用基类的绘制函数绘制文本
        // QStyledItemDelegate::paint(painter, option, index);
        painter->restore();
    }
}
void CustomStyledItemDelegate::paint1(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    QRect rect = option.rect;
    QFontMetrics fm = painter->fontMetrics();

    // 假设数据格式为 "列1|列2|列3|列4"
    QStringList columns = index.data().toString().split("|");
    if (columns.size() == 5) {
        QString secondColumn = columns[2].trimmed();
        QString thirdColumn = columns[3].trimmed();

        //根据不同状态，绘制不同颜色边框
        if(option.state.testFlag(QStyle::State_Selected))
        {
            painter->setPen(QPen(Qt::white));
            painter->setBrush(QColor(229, 241, 255));
            painter->setBrush(QColor(15,128,217));
            painter->drawRoundedRect(rect,7,7);
        }
        else if(option.state.testFlag(QStyle::State_MouseOver))
        {
            painter->setPen(QPen(Qt::green));
            painter->setBrush(Qt::NoBrush);
            painter->drawRoundedRect(rect,7,7);
        }
        else{
            painter->setPen(QPen(Qt::black));
            painter->setBrush(Qt::NoBrush);
            painter->drawRoundedRect(rect,7,7);
        }

        painter->setPen(QPen(Qt::black));
            // 绘制第二列
        painter->drawText(rect.left(), rect.top(), rect.width() / 2, rect.height(), Qt::AlignLeft | Qt::AlignVCenter, secondColumn);

        // 绘制第三列
        painter->drawText(rect.left() + rect.width() / 2, rect.top(), rect.width() / 2, rect.height(), Qt::AlignLeft | Qt::AlignVCenter, thirdColumn);
    }
}
void CustomStyledItemDelegate::paint2(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    if(index.isValid())
    {
        //获取单元格中数据
        QVariant variant = index.data(Qt::UserRole+1);
        ItemData data = variant.value<ItemData>();
        //单元格边框，注意要比单元格总大小略小一些，不然绘制的边框线刚好处于外缘会显示不全
        QRect rect = QRect(option.rect.left()+3,option.rect.top()+3,option.rect.width()-6,option.rect.height()-6);
        // QRect rect = option.rect;
        //根据不同状态，绘制不同颜色边框
        if(option.state.testFlag(QStyle::State_Selected))
        {
            painter->setPen(QPen(Qt::blue));
            painter->setBrush(QColor(229, 241, 255));
            painter->drawRoundedRect(rect,7,7);
        }
        else if(option.state.testFlag(QStyle::State_MouseOver))
        {
            painter->setPen(QPen(Qt::green));
            painter->setBrush(Qt::NoBrush);
            painter->drawRoundedRect(rect,7,7);
        }
        else{
            painter->setPen(QPen(Qt::gray));
            painter->setBrush(Qt::NoBrush);
            painter->drawRoundedRect(rect,7,7);
        }

        //绘制数据位置
        // QRect dmRect = QRect(210+option.rect.left(),10+option.rect.top(),330,50);
        // QRect cnameRect = QRect(210+option.rect.left(),65+option.rect.top(),330,40);
        // QRect enameRect = QRect(210+option.rect.left(),100+option.rect.top(),330,40);
        // QRect picRect = QRect(option.rect.left()+5,option.rect.top()+10,210,120);

        QRect seqRect = QRect(option.rect.left()+6 ,12+option.rect.top(),100,20);
        QRect dmRect = QRect(10+option.rect.left(),10+option.rect.top(),130,20);
        QRect cnameRect = QRect(10+option.rect.left(),8+option.rect.top(),530,20);
        QRect enameRect = QRect(10+option.rect.left(),100+option.rect.top(),330,40);
        QRect picRect = QRect(option.rect.left()+5,option.rect.top()+10,210,120);

        QStringList columns = index.data().toString().split("|");
        if (columns.size() == 5) {
            QString firstColumn = columns[0].trimmed();
            QString secondColumn = columns[2].trimmed();
            QString thirdColumn = columns[3].trimmed();
            painter->setPen(QPen(Qt::black));

            painter->setFont(QFont("Times", 10));
            painter->drawText(seqRect,Qt::AlignLeft,firstColumn ); //序号

            painter->setFont(QFont("Times", 12, QFont::Bold));
            painter->drawText(dmRect,Qt::AlignCenter,secondColumn); //国家代码

            painter->setFont(QFont("微软雅黑", 12));
            painter->drawText(cnameRect,Qt::AlignCenter,thirdColumn); //国家中文名

            // painter->setFont(QFont("Times", 10));
            // painter->drawText(enameRect,Qt::AlignCenter,firstColumn ); //国家英文名

            // painter->drawPixmap(picRect,data.tp);
        }
    }
}
QSize CustomStyledItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    // QSize size = QStyledItemDelegate::sizeHint(option, index);
    // return QSize(size.width(), size.height()*6); // 可以根据需要调整大小
    return QSize(150, 40);//width,height
}
